package com.api.smoke.wall;

import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.api.test.VoidContextLoader;
import com.api.util.AssertUtil;
import com.api.util.DataUtil;
import com.api.util.GenericUtils.GenericType;
import com.api.util.repo.DtoFactory;
import com.api.util.repo.impl.DomainUser;
import com.zyncro.api.IWallApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.CollectionResultApi;
import com.zyncro.api.model.EventApi;

@ContextConfiguration(loader = VoidContextLoader.class)
public class GetChildEventsSmoke extends AbstractApiTest {

	private CollectionResultApi<EventApi> expectedResults;

	private String eventUrn;

	@BeforeClass
	protected void setUp() throws Exception {

		final String domainName = this.getClass().getSimpleName();
		DomainUser mainAdmin = getRepo().getMainUser(domainName);

		getApi().login(mainAdmin.getAppId(), mainAdmin.getPassword());

		eventUrn = getRepo().getPropertyDto(domainName + "-EventUrn", new DtoFactory<String>() {

			public String createDto() {

				try {
					String event = getApi().getService(IWallApiService.class).publishInCompanyFeed("comment", null, null, null, null, null,
							false, false);

					getApi().getService(IWallApiService.class).publishInCompanyFeed("comment2", null, null, null, eventUrn, null, false,
							false);
					getApi().getService(IWallApiService.class).publishInCompanyFeed("comment3", null, null, null, eventUrn, null, false,
							false);
					getApi().getService(IWallApiService.class).publishInCompanyFeed("comment4", null, null, null, eventUrn, null, false,
							false);
					getApi().getService(IWallApiService.class).publishInCompanyFeed("comment5", null, null, null, eventUrn, null, false,
							false);
					getApi().getService(IWallApiService.class).publishInCompanyFeed("comment6", null, null, null, eventUrn, null, false,
							false);
					getApi().getService(IWallApiService.class).publishInCompanyFeed("comment7", null, null, null, eventUrn, null, false,
							false);
					return event;

				} catch (Exception e) {
					throw new IllegalStateException("Exception creating test data", e);
				}
			}
		});

		String fileName = getRepo().getPropertyDto(domainName + "-Events", new DtoFactory<String>() {

			public String createDto() {

				String env = System.getProperty("env");
				String fileName = String.format("./json/%s/%s.json", env, domainName);

				try {
					CollectionResultApi<EventApi> result = getApi().getService(IWallApiService.class).getChildEvents(eventUrn, null, 0,
							false, false, null);

					DataUtil.saveDtoToFile(result, fileName);
				} catch (Exception e) {

					throw new IllegalStateException("Exception creating test data", e);
				}
				return fileName;
			}
		});

		this.expectedResults = DataUtil.getDtoFromFile(fileName, new GenericType<CollectionResultApi<EventApi>>() {
		});
	}

	@Test
	public void testGetChildEvents() throws ZyncroApiException {

		CollectionResultApi<EventApi> result = getApi().getService(IWallApiService.class).getChildEvents(eventUrn, null, 0, false, false,
				null);

		AssertUtil.assertDTOEquals(result.getElements(), expectedResults.getElements());
	}
}
